{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import glob\n",
    "import numpy as np\n",
    "from shutil import copyfile\n",
    "symlink = True    # If this is false the files are copied instead"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Combining CINIC-10 train and validation sets\n",
    "This notebook serves as a tool to combine CINIC-10's train and validation sets into a new train set (larger). This configuration better matches CIFAR-10.\n",
    "\n",
    "#### ENSURE THAT CINIC-10 IS DOWNLOADED AND STORED IN ../data/cinic-10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "cinic_directory = \"../data/cinic-10\"\n",
    "enlarge_directory = \"../data/cinic-10-trainlarge\"\n",
    "classes = [\"airplane\", \"automobile\", \"bird\", \"cat\", \"deer\", \"dog\", \"frog\", \"horse\", \"ship\", \"truck\"]\n",
    "sets = ['train', 'valid', 'test']\n",
    "if not os.path.exists(enlarge_directory):\n",
    "    os.makedirs(enlarge_directory)\n",
    "if not os.path.exists(enlarge_directory + '/train'):\n",
    "    os.makedirs(enlarge_directory + '/train')\n",
    "if not os.path.exists(enlarge_directory + '/test'):\n",
    "    os.makedirs(enlarge_directory + '/test')\n",
    "    \n",
    "for c in classes:\n",
    "    if not os.path.exists('{}/train/{}'.format(enlarge_directory, c)):\n",
    "        os.makedirs('{}/train/{}'.format(enlarge_directory, c))\n",
    "    if not os.path.exists('{}/test/{}'.format(enlarge_directory, c)):\n",
    "        os.makedirs('{}/test/{}'.format(enlarge_directory, c))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "for s in sets:\n",
    "    for c in classes:\n",
    "        source_directory = '{}/{}/{}'.format(cinic_directory, s, c)\n",
    "        filenames = glob.glob('{}/*.png'.format(source_directory))\n",
    "        for fn in filenames:\n",
    "            dest_fn = fn.split('/')[-1]\n",
    "            if s == 'train' or s == 'valid':\n",
    "                dest_fn = '{}/train/{}/{}'.format(enlarge_directory, c, dest_fn)\n",
    "                if symlink:\n",
    "                    if not os.path.islink(dest_fn):\n",
    "                        os.symlink(fn, dest_fn)\n",
    "                else:\n",
    "                    copyfile(fn, dest_fn)\n",
    "                \n",
    "                \n",
    "            elif s == 'test':\n",
    "                dest_fn = '{}/test/{}/{}'.format(enlarge_directory, c, dest_fn)\n",
    "                if symlink:\n",
    "                    if not os.path.islink(dest_fn):\n",
    "                        os.symlink(fn, dest_fn)\n",
    "                else:\n",
    "                    copyfile(fn, dest_fn)\n",
    "                    "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
